/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Dao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;

/**
 *
 * @author Rodolfo
 */
public class Singleton {
    
    private static Connection con;
    
    private Singleton(){
         con = null;  
    }
    
    public static boolean connect(String host, int port, String db, String user, String pass) 
    {
        try {
            if (con==null){
                Class.forName("org.postgresql.Driver");
                con = DriverManager.getConnection("jdbc:postgresql://"+host+":"+port+"/"+db+"",user,pass);
            }
           
               return(con!=null);              
                        
        } catch (Exception e) {
            return(false);
        }
            
    }
    
    public static boolean disconnect()
    {
        try {
            con.close();
            return (true);
            
        } catch (Exception e) {
            return(false);
        }
    }
    
    public static boolean executeSQL(String sql){
        try {
            Statement stm = con.createStatement();
            stm.execute(sql);
            return(true);
        } catch (Exception e) {
          e.printStackTrace();
            return(false);
        }
    }
    
    public static Statement executeSQLStatement(String sql){
        try {
            Statement stm = con.createStatement();
            stm.execute(sql);
            return(stm); //vai retornar conjunto para o caso de um select, por exemplo
        } catch (Exception e) {
            return(null);
        }
    }
    
} 
